/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.acme.server;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author iiping
 */
@SuppressWarnings("serial")
public class ServerAdmController extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
      UserService userService = UserServiceFactory.getUserService();

      
      

      if(request.getRequestURI().equals("/admin/add")){
        frmAdd(request, response);
      } else if(request.getRequestURI().equals("/admin/updateOrDelete")){
        if(request.getParameter("ax").equals("update")){
          frmUpdate(request, response);
        } else if(request.getParameter("ax").equals("delete")){
          frmDelete(request, response);
        }
      }
      
      request.setAttribute("authServers", DAOCentral.getInstance().getServers());
      request.setAttribute("logoutURL", userService.createLogoutURL(request.getRequestURI()));

      RequestDispatcher rd = request.getRequestDispatcher("/views/SvrAdmIndex.jsp");
      rd.forward(request, response);
      
    }

    protected void frmAdd(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

      String ipAddress = request.getParameter("ipAddress");
      String accessCode = request.getParameter("accessCode");
      
      if(null != ipAddress && null != accessCode){
        try{

          TOServer svr = new TOServer(ipAddress, accessCode);

          //before adding try to query first
          DAOCentral ds = DAOCentral.getInstance();
          if(!ds.isServerAuthenticated(svr)){
            ds.addAuthServer(svr);
            request.setAttribute("msgStat", "added");
          } else{
            request.setAttribute("msgStat", "duplicate found");
          }

          
        }
        catch(Throwable e){
        	e.printStackTrace();
          request.setAttribute("msgStat", "an error occurred while adding");
        }
      }
      else{
        request.setAttribute("msgStat", "Invalid Parameter found during adding");
      }
    }

    protected void frmUpdate(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

      //parameters are
      String ipAddressOrig 	= request.getParameter("ipAddressOrig");
      String accessCodeOrig = request.getParameter("accessCodeOrig");
      String accessCode 	= request.getParameter("accessCode");
      String ipAddress 		= request.getParameter("ipAddress");

      if(null != ipAddress && null != ipAddressOrig && null != accessCode && null != accessCodeOrig){
        try{

          TOServer svrOrig = new TOServer(ipAddressOrig);
          svrOrig.setAuthCodeNoEnc(accessCodeOrig);
          TOServer svrUpd = new TOServer(ipAddress, accessCode);
          
          //before adding try to query first
          DAOCentral ds = DAOCentral.getInstance();

          ds.updateAuthServer(svrOrig, svrUpd);
          request.setAttribute("msgStat", "updated successfully");
        }
        catch(Throwable e){
          request.setAttribute("msgStat", "an error occurred while adding");
        }
      }
      else{
        request.setAttribute("msgStat", "Invalid Parameter found during update");
      }
      

    }

    protected void frmDelete(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
      String ipAddress = request.getParameter("ipAddress");
      String accessCode = request.getParameter("accessCode");

      if(null != ipAddress && null != accessCode){
        try{

          TOServer svr = new TOServer(ipAddress);
          svr.setAuthCodeNoEnc(accessCode);

          //before adding try to query first
          DAOCentral ds = DAOCentral.getInstance();
          if(ds.isServerAuthenticated(svr)){
            ds.deleteAuthServer(svr);
            request.setAttribute("msgStat", "delete successful");
          } else{
            request.setAttribute("msgStat", "can't delete non-existent");
          }
        }
        catch(Throwable e){
          request.setAttribute("msgStat", "an error occurred while removing");
        }
      }
      else{
        request.setAttribute("msgStat", "Invalid Parameter found during removing");
      }
    }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
